package v1

import (
	"github.com/gin-gonic/gin"
	"ota/bll"
	"ota/model"
	"ota/server/web/middleware"
	"ota/utils"
)

var UserUpgrade = &userUpgrade{}

func init() {
	RegisterRouter(UserUpgrade)
}

type userUpgrade struct{}

// Init
func (a *userUpgrade) Init(r *gin.RouterGroup) {
	g := r.Group("/userUpgrade", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/retry", a.retry)
		g.POST("/statistics", a.statistics)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)
		g.POST("/detail", a.find)
	}
}

// create
func (a *userUpgrade) create(c *gin.Context) {
	var (
		in  = &model.UserUpgradeCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.UserUpgrade.Create(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// update
func (a *userUpgrade) update(c *gin.Context) {
	var (
		in  = &model.UserUpgradeUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if err = bll.UserUpgrade.Update(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// update
func (a *userUpgrade) retry(c *gin.Context) {
	var (
		in  = &model.UserUpgradeRetryRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if err = bll.UserUpgrade.Retry(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// list
func (a *userUpgrade) statistics(c *gin.Context) {
	var (
		in  = &model.UserUpgradeStatisticsRequest{}
		out = &model.UserUpgradeStatisticsItem{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if out, err = bll.UserUpgrade.Statistics(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// list
func (a *userUpgrade) list(c *gin.Context) {
	var (
		in  = &model.UserUpgradeListRequest{}
		out = &model.UserUpgradeListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if out, err = bll.UserUpgrade.List(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// list
func (a *userUpgrade) find(c *gin.Context) {
	var (
		in  = &model.UserUpgradeInfoRequest{}
		out = &model.UserUpgradeInfo{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if out, err = bll.UserUpgrade.Find(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// delete
func (a *userUpgrade) delete(c *gin.Context) {
	var (
		in  = &model.UserUpgradeDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.UserUpgrade.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}
